package com.dwusoft.sendmsg.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.dwusoft.sendmsg.common.entity.TSmsService;
import com.dwusoft.sendmsg.manager.TSmsServiceManager;

public class TSmsServiceManagerImpl implements TSmsServiceManager {

	protected IDao smsDao;
	protected final String entityName = "com.dwusoft.sendmsg.common.entity.TSmsService";

	public void setSmsDao(IDao smsDao) {
		this.smsDao = smsDao;
	}

	public String saveInfo(ParamsVo<TSmsService> vo) throws BusinessException {
		TSmsService tSmsService = vo.getObj();
		String cacheKey = "";
		//String cacheKey="tSmsService_"+infoId;
		if(Validator.isNull(tSmsService.getSmsId())){
		    String infoId = Validator.generate();
		    //定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		   tSmsService.setSmsId(infoId);
		}
		MethodParam param = new MethodParam("TSmsService", cacheKey, "", entityName);
		param.setVaule(tSmsService);
		if (smsDao.insert(param)) {
			return tSmsService.getSmsId();
		}
		return "";
	}

	public boolean editInfo(ParamsVo<TSmsService> vo) throws BusinessException {
		TSmsService tSmsService = vo.getObj();
		if (Validator.isNotNull(tSmsService.getSmsId())) {
			String cacheKey = "";
			//String cacheKey="tSmsService_"+tSmsService.getSmsId();
			MethodParam param = new MethodParam("TSmsService", cacheKey, "", entityName);
			param.setVaule(tSmsService);
			return smsDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<TSmsService> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="tSmsService_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		TSmsService info = (TSmsService) smsDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("TSmsService");
			return smsDao.edit(param);
		} else {
			param.setParams("smsId", infoId);
			param.setDelete(true);
			return smsDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<TSmsService> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="tSmsService_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return smsDao.selectById(param);
	}

	public Object execute(ParamsVo<TSmsService> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<TSmsService> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "100"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		//param.setParams("orderBy", "create_Time desc");
		Page page = smsDao.pageSelect(param);
		return page;
	}

	public List<TSmsService> getList(ParamsVo<TSmsService> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return smsDao.select(param);
	}

	public long countInfo(ParamsVo<TSmsService> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return smsDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<TSmsService> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(smsDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		TSmsService tSmsService = vo.getObj();
		if(!Validator.isEmpty(tSmsService)){
	    if(Validator.isNotNull(tSmsService.getSmsId())){
	        sb.append(" and smsId = :smsId");
			param.setParams("smsId", tSmsService.getSmsId());
	    }
	    if(Validator.isNotNull(tSmsService.getCreateTime())){
	        sb.append(" and createTime = :createTime");
			param.setParams("createTime", tSmsService.getCreateTime());
	    }
	    if(Validator.isNotNull(tSmsService.getDelStatus())){
	        sb.append(" and delStatus = :delStatus");
			param.setParams("delStatus", tSmsService.getDelStatus());
	    }
	    if(Validator.isNotNull(tSmsService.getCreateUser())){
	        sb.append(" and createUser = :createUser");
			param.setParams("createUser", tSmsService.getCreateUser());
	    }
	    if(Validator.isNotNull(tSmsService.getModifyTime())){
	        sb.append(" and modifyTime = :modifyTime");
			param.setParams("modifyTime", tSmsService.getModifyTime());
	    }
	    if(Validator.isNotNull(tSmsService.getModifyUser())){
	        sb.append(" and modifyUser = :modifyUser");
			param.setParams("modifyUser", tSmsService.getModifyUser());
	    }
	    if(Validator.isNotNull(tSmsService.getSmsName())){
	        sb.append(" and smsName = :smsName");
			param.setParams("smsName", tSmsService.getSmsName());
	    }
	    if(Validator.isNotNull(tSmsService.getSmsAddress())){
	        sb.append(" and smsAddress = :smsAddress");
			param.setParams("smsAddress", tSmsService.getSmsAddress());
	    }
	    if(Validator.isNotNull(tSmsService.getSmsShortName())){
	        sb.append(" and smsShortName = :smsShortName");
			param.setParams("smsShortName", tSmsService.getSmsShortName());
	    }
	    if(Validator.isNotNull(tSmsService.getSmsSn())){
	        sb.append(" and smsSn = :smsSn");
			param.setParams("smsSn", tSmsService.getSmsSn());
	    }
	    if(Validator.isNotNull(tSmsService.getSmsPwd())){
	        sb.append(" and smsPwd = :smsPwd");
			param.setParams("smsPwd", tSmsService.getSmsPwd());
	    }
        }
		param.setSqlStr(sb.toString());
		return param;
	}
}

